package view.interfaces;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import business.dao.SaboresDAO;
import business.model.SaboresTO;
import business.model.tableModel.SaboresTableModel;
import exception.BusinessException;
import exception.InterfaceException;
import exception.UsuarioException;

public class ListarPizzas extends JFrame {

	public JFrame frmPizza;
	private JTable tblLista;
	private JTextField txtSabor;
	private SaboresDAO saboresDAO = new SaboresDAO();
	private SaboresTO saboresTO;
	private SaboresTableModel saboresTableModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListarPizzas window = new ListarPizzas();
					window.frmPizza.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ListarPizzas() {
		initialize();
	}

	public void listar() {
		try {
			saboresTableModel = new SaboresTableModel(saboresDAO.listar(null));
			tblLista.setModel(saboresTableModel);
		} catch (UsuarioException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// saboresDAO.listar(null);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmPizza = new JFrame();
		frmPizza.setTitle("Pizza");
		frmPizza.setBounds(100, 100, 600, 429);
		frmPizza.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JScrollPane scrollPane = new JScrollPane();

		JPanel panel = new JPanel();

		JButton btnNewButton_1 = new JButton("Excluir");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (tblLista.getSelectedRow() >= 0) {
					try {
						SaboresTO to = saboresTableModel.sabores.get(tblLista
								.getSelectedRow());
						saboresDAO.excluir(to);
						listar();
					} catch (UsuarioException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (BusinessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterfaceException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});

		JButton button = new JButton("VOLTAR");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new telaAdministrador().setVisible(true);
				frmPizza.setVisible(Boolean.FALSE);
			}
		});
		button.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroupLayout groupLayout = new GroupLayout(frmPizza.getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								Alignment.LEADING,
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(10)
																		.addComponent(
																				button,
																				GroupLayout.PREFERRED_SIZE,
																				89,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				390,
																				Short.MAX_VALUE)
																		.addComponent(
																				btnNewButton_1)
																		.addGap(22))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								scrollPane,
																								GroupLayout.PREFERRED_SIZE,
																								561,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								panel,
																								GroupLayout.PREFERRED_SIZE,
																								561,
																								GroupLayout.PREFERRED_SIZE))
																		.addContainerGap(
																				13,
																				Short.MAX_VALUE)))));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.TRAILING)
				.addGroup(
						groupLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(panel,
										GroupLayout.PREFERRED_SIZE, 64,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED,
										28, Short.MAX_VALUE)
								.addComponent(scrollPane,
										GroupLayout.PREFERRED_SIZE, 238,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										groupLayout
												.createParallelGroup(
														Alignment.BASELINE)
												.addComponent(btnNewButton_1)
												.addComponent(button))
								.addGap(21)));

		JLabel lblSabor = new JLabel("Sabor:");
		lblSabor.setFont(new Font("Tahoma", Font.BOLD, 11));

		txtSabor = new JTextField();
		txtSabor.setColumns(10);

		JButton btnNewButton = new JButton("Pesquisar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {
					SaboresTO saboresTO = new SaboresTO();
					saboresTO.setDescricao(txtSabor.getText());
					saboresTableModel = new SaboresTableModel(saboresDAO
							.listar(saboresTO));
					tblLista.setModel(saboresTableModel);
				} catch (UsuarioException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterfaceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		JButton btnNewButton_2 = new JButton("Nova Pizza");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new telaPizza().setVisible(true);
				frmPizza.setVisible(Boolean.FALSE);
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblSabor, GroupLayout.PREFERRED_SIZE, 44,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(txtSabor, GroupLayout.PREFERRED_SIZE,
								270, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(btnNewButton)
						.addPreferredGap(ComponentPlacement.RELATED, 45,
								Short.MAX_VALUE).addComponent(btnNewButton_2)
						.addGap(18)));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(22)
								.addComponent(lblSabor,
										GroupLayout.PREFERRED_SIZE, 19,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(23, Short.MAX_VALUE))
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap(20, Short.MAX_VALUE)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														txtSabor,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(btnNewButton)
												.addComponent(btnNewButton_2))
								.addGap(21)));
		panel.setLayout(gl_panel);

		tblLista = new JTable();
		listar();
		scrollPane.setViewportView(tblLista);
		frmPizza.getContentPane().setLayout(groupLayout);
	}
}
